package com.demo.demo.controller;

import com.demo.demo.config.IRedisService;
import com.demo.demo.entity.Class;
import com.demo.demo.service.ClassService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("class")
@Controller
public class ClassController {
    @Autowired
    private ClassService classService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private IRedisService redisService;
    @RequestMapping("/add/{key}/{value}")
    @ResponseBody
    private String add(@PathVariable String key ,@PathVariable String value){
        stringRedisTemplate.opsForValue().set(key,value);
        return "0";
    }
    @RequestMapping("/get/{key}")
    @ResponseBody
    private String get(@PathVariable String key){
        return (String) redisService.get(key);
    }
    @RequestMapping("/findClassById/{id}")
    private String findClassById(Model model, @PathVariable String id)throws Exception{
        Class classInfo =null;
        classInfo = (Class) redisTemplate.opsForValue().get(id);
        if (classInfo != null){
            System.out.println("from redis");
        }else {
            System.out.println("from mysql");
            classInfo = classService.selectClassById(id);
            if (!StringUtils.isEmpty(classInfo))
                redisTemplate.opsForValue().set(classInfo.getId(),classInfo);
        }
        model.addAttribute("class",classInfo);

        return "html/class/findClassById";
    }

    @RequestMapping(value="/findAllClass")
    @ResponseBody
    public PageInfo<Class> findAllClass() throws Exception{
        PageHelper.startPage(0, 5);

        List<Class> selectAllClass = classService.selectAllClassDistinct();
        for (Class class1 : selectAllClass) {
            System.out.println(class1);
        }
        PageInfo<Class> pageInfo = new PageInfo<Class>(selectAllClass);
        return pageInfo;
    }

    @RequestMapping("/del/{key}")
    @ResponseBody
    public Boolean del(@PathVariable String key)throws  Exception{
        Boolean delete = redisTemplate.delete(key);
        System.out.println(delete);
        System.out.println("delete");
        return delete;
    }

    @RequestMapping("/findClass/{id}")
    @ResponseBody
    Class findClass( @PathVariable String id)throws Exception{
      return classService.selectClassById(id);
    }

    @RequestMapping("update/{id}/{name}")
    @ResponseBody
    String updateClass(@PathVariable String id ,@PathVariable String name) throws Exception{
        Class classInfo = new Class();
        classInfo.setId(id);
        classInfo.setClassname(name);
         classService.updateClass(classInfo);
         return  "update";
    }

}
